#include <iostream>
#include <string>
#include "Lazy.hpp"

// 用于测试的简单函数
int expensive_computation(int x, int y)
{
    std::cout << "执行计算..." << std::endl;
    return x + y;
}

std::string create_message(const std::string &name)
{
    std::cout << "创建消息..." << std::endl;
    return "Hello, " + name + "!";
}

// 测试用例集合
void run_lazy_tests()
{
    std::cout << "\n=== Lazy 类测试开始 ===\n"
              << std::endl;

    // 测试1：基本延迟计算功能
    {
        std::cout << "测试1：基本延迟计算" << std::endl;
        auto lazy_sum = lazy(expensive_computation, 10, 20);
        std::cout << "lazy_sum 已创建，但计算还未执行" << std::endl;

        // 验证值未被创建
        std::cout << "IsValueCreated: " << (lazy_sum.IsValueCreated() ? "true" : "false") << std::endl;

        // 第一次访问，将触发计算
        std::cout << "第一次访问 Value()..." << std::endl;
        int result = lazy_sum.Value();
        std::cout << "计算结果: " << result << std::endl;

        // 再次访问，不会重复计算
        std::cout << "第二次访问 Value()..." << std::endl;
        result = lazy_sum.Value();
        std::cout << "计算结果: " << result << std::endl;
    }

    // 测试2：使用字符串处理演示延迟求值
    {
        std::cout << "\n测试2：字符串处理延迟求值" << std::endl;
        auto lazy_greeting = lazy(create_message, std::string("Alice"));
        std::cout << "lazy_greeting 已创建，但消息还未生成" << std::endl;

        // 只有在实际需要时才会创建消息
        std::cout << "获取消息..." << std::endl;
        std::string message = lazy_greeting.Value();
        std::cout << "消息内容: " << message << std::endl;
    }

    // 测试3：验证缓存功能
    {
        std::cout << "\n测试3：验证缓存功能" << std::endl;
        int counter = 0;
        auto lazy_counter = lazy([&counter]()
                                 {
            counter++;
            std::cout << "增加计数器..." << std::endl;
            return counter; });

        std::cout << "第一次访问: " << lazy_counter.Value() << std::endl;
        std::cout << "第二次访问: " << lazy_counter.Value() << std::endl;
        std::cout << "第三次访问: " << lazy_counter.Value() << std::endl;
        std::cout << "注意：计数器只增加了一次，说明结果被缓存了" << std::endl;
    }

    std::cout << "\n=== Lazy 类测试结束 ===\n"
              << std::endl;
}

int main()
{
    run_lazy_tests();
    return 0;
}
